<template>
	<view class="huicontainer">

		<view class="uni-padding-wrap uni-common-mt">

			<view class="loginbtn">
				<view>第一步：</view>
				<button type="primary" :disabled="!isgo" open-type="getUserInfo" @click="wxLogin">获取微信信息</button>
				<view>第二步：</view>
				<button type="primary" class="mt-20" :disabled="isgo" open-type="getPhoneNumber"
					@getphonenumber="phonelogin">手机号登录</button>
			</view>

		</view>
		<foot />
	</view>

</template>
<script>
	var app = getApp();
	export default {
		data() {
			return {
				code: '',
				isgo: true,
				model: {},
				openid: undefined
			}
		},
		onLoad() {},
		methods: {
			phonelogin: function(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					this.$request.post(this.$api.user.phonelogin, {
						encrypted: e.detail.encryptedData,
						iv: e.detail.iv,
						name: this.model.name,
						picture: this.model.picture,
						province: this.model.province,
						city: this.model.city,
						district: this.model.district,
						sex: this.model.sex,
						openid: this.openid,
						unionid: this.model.unionid
					}).then(res => {
						this.$storage.setData('token', res.token);
						this.$storage.setJson('user', res.data);
						uni.navigateBack({
							delta: 1,
							success: function(e) {
								var page = getCurrentPages().pop();
								if (page == undefined || page == null) return;
								page.onLoad();
							}
						})
					});
				}
			},
			getopen: function(e) {
				var that = this
				uni.login({
					provider: 'weixin',
					success: function(res) {
						uni.getUserInfo({
							provider: 'weixin',
							success: infoRes => {
								that.model = infoRes.userInfo
								that.$request.post(that.$api.user.getopen, {
									code: res.code
								}, 'form').then(res => {
									that.openid = res.data
									that.isgo = false
								})
							}
						});

					}
				});
			},
			wxGetUserProfile: function() {
				return new Promise((resolve, reject) => {
					uni.getUserProfile({
						lang: 'zh_CN',
						desc: '用于完善会员资料',
						success: (res) => {
							resolve(res)
						},
						// 失败回调
						fail: (err) => {
							reject(err)
						}
					})
				})
			},

			wxSilentLogin: function() {
				return new Promise((resolve, reject) => {
					uni.login({
						success(res) {
							resolve(res.code)
						},
						fail(err) {
							reject(err)
						}
					})
				})
			},
			wxLogin: function(e) {
				var that = this
				let p1 = this.wxSilentLogin()
				let p2 = this.wxGetUserProfile()
				Promise.all([p1, p2]).then((res) => {
					let code = res[0]
					let iv = res[1].iv
					let encryptedData = res[1].encryptedData
					// 请求服务器
					that.$request.post(that.$api.user.getopen, {
						code: code,
						encrypted: res[1].encryptedData,
						iv: res[1].iv,
						rawData: res[1].rawData,
						signature: res[1].signature
					}).then(res => {
						console.log(res)
						that.model = res.data
						that.openid = res.data.openid
						that.isgo = false
					})
				}).catch((err) => {
					console.log(err)
				})
			}
		}
	}
</script>

<style>
	.logo {
		margin-top: 100rpx;
		text-align: center;
	}

	.logo image {
		width: 300rpx;
		height: 300rpx;
	}

	.loginbtn {
		width: 80%;
		margin: 200rpx auto 0 auto;
	}
</style>
